﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BaiDuMusic
{
    /// <summary>
    /// 百度 歌曲信息控制
    /// </summary>
    public class BaiDuSong
    {
        public string SongID { get; set; }
        /// <summary>
        /// 当前操作的歌曲的对象
        /// </summary>
        public SongData CurrentSong = null;
        public BaiDuSong(string SongID, bool AutoLoad = true)
        {
            this.SongID = SongID;
            if (AutoLoad)
                LoadData();
        }
        /// <summary>
        /// 加载Web歌曲信息
        /// </summary>
        /// <returns></returns>
        public SongData LoadData()
        {
            string url = GetSongUrl();
            try
            {
                string result = NetHelper.Get(url);
                JObject obj = JObject.Parse(result);
                JToken token = obj["data"]["songList"].First();

                SongData data = JsonConvert.DeserializeObject<SongData>(token.ToString());
                CurrentSong = data;
                return data;
            }
            catch (Exception ex)
            {
                throw new Exception("加载Web歌曲失败", ex);
            }
        }
        /// <summary>
        /// /获取歌曲地址
        /// </summary>
        /// <returns></returns>
        public string GetSongUrl()
        {
            string url = string.Format("http://music.baidu.com/data/music/links?songIds={0}", this.SongID);
            return url;
        }
        /// <summary>
        /// 将Web歌曲信息，下载到本地
        /// 1.歌曲文件
        /// 2.歌词文件
        /// 3.歌曲大图
        /// 4.歌曲小图
        /// </summary>
        public void DownloadData()
        {

        }
    }
    /// <summary>
    /// 查询的歌曲数据
    /// </summary>
    public class SongData
    {
        public string SongID { get; set; }
        /// <summary>
        /// 歌曲名称
        /// </summary>
        public string SongName { get; set; }
        public string ArtistID { get; set; }
        /// <summary>
        /// 歌曲作者
        /// </summary>
        public string ArtistName { get; set; }
        public string AlbumID { get; set; }
        /// <summary>
        /// 歌曲专辑
        /// </summary>
        public string AlbumName { get; set; }

        /// <summary>
        /// 歌曲小图
        /// </summary>
        public string SongPicSmall { get; set; }
        /// <summary>
        /// 歌曲大图
        /// </summary>
        public string SongPicBig { get; set; }
        /// <summary>
        /// 暂不使用
        /// </summary>
        public string SongPicRadio { get; set; }
        /// <summary>
        /// 歌词地址
        /// </summary>
        public string LrcLink { get; set; }
        public string Version { get; set; }
        public int Time { get; set; }
        public string LinkCode { get; set; }
        /// <summary>
        /// 歌曲地址
        /// </summary>
        public string SongLink { get; set; }
        public string ShowLink { get; set; }
        /// <summary>
        /// 歌曲文件格式
        /// </summary>
        public string Format { get; set; }
        /// <summary>
        /// 比特率
        /// </summary>
        public string Rate { get; set; }
        /// <summary>
        /// 歌曲文件大小
        /// </summary>
        public int Size { get; set; }
        public string RelateStatus { get; set; }
        public string ResourceType { get; set; }
        public string Source { get; set; }
    }
}
